package cn.cmdr.controller;

import cn.cmdr.pojo.*;
import cn.cmdr.service.JournalService;
import cn.cmdr.service.SupComService;
import cn.cmdr.service.SupplierService;
import cn.cmdr.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sup/sup")
public class SupplierController {
    @Resource
    private SupplierService supplierService;
    @Resource
    private SupComService supComService;
    @Resource
    private JournalService journalService;

    @PostMapping("/getSupp")
    public ResultVo getSupp(@RequestBody Commodity commodity){
        return ResultVo.sucess(supplierService.getSupplier(commodity.getBrandId()));
    }

    @PostMapping("/getSupCom")
    public ResultVo getSupCom(@RequestBody Commodity commodity){
        return ResultVo.sucess(supComService.getSupCom(commodity.getSupplierId()));
    }

    @PostMapping("/list/{pageSize}/{pageNo}")
    public ResultVo getList(@PathVariable Integer pageSize, @PathVariable Integer pageNo,@RequestBody Supplier supplier){
        return ResultVo.sucess(supplierService.getSupPage(pageNo,pageSize,supplier));
    }

    @PostMapping("/suplist/{suppId}")
    public ResultVo supList(@PathVariable Integer suppId){
        List<SupCom> list = supComService.getList(suppId);
        return ResultVo.sucess(list,list.size());
    }

    @PostMapping("/add")
    @RequiresPermissions("sup:add")
    public ResultVo add(@RequestBody Supplier supplier){
        try {
            supplierService.add(supplier);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("添加供应商:"+supplier.getName());
            journal.setMenuId(11);
            journalService.add(journal);
            return ResultVo.sucess("添加成功！！");
        }catch (Exception e){
            return ResultVo.fail("添加失败！",e);
        }
    }
    @PostMapping("/addCom")
    @RequiresPermissions("supcom:add")
    public ResultVo addCom(@RequestBody SupCom supCom){
        try {
            supComService.add(supCom);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("添加供应商货物:"+supCom.getName());
            journal.setMenuId(11);
            journalService.add(journal);
            return ResultVo.sucess("添加成功！！");
        }catch (Exception e){
            return ResultVo.fail("添加失败！",e);
        }
    }
    @PostMapping("/getcredit")
    public ResultVo getCredit(){
        return ResultVo.sucess(supplierService.getCredit());
    }

    @PostMapping("/update")
    @RequiresPermissions("sup:update")
    public ResultVo update(@RequestBody Supplier supplier){
        try {
            supplierService.updateById(supplier);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("修改供应商:"+supplier.getName());
            journal.setMenuId(11);
            journalService.add(journal);
            return ResultVo.sucess("修改成功！！");
        }catch (Exception e){
            return ResultVo.fail("修改失败",e);
        }
    }

    @PostMapping("/updateCom")
    @RequiresPermissions("supcom:update")
    public ResultVo updateCom(@RequestBody SupCom supCom){
        try {
            supComService.update(supCom);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("修改供应商货物:"+supCom.getName());
            journal.setMenuId(11);
            journalService.add(journal);
            return ResultVo.sucess("修改成功！！");
        }catch (Exception e){
            return ResultVo.fail("修改失败！",e);
        }
    }

    @GetMapping("/delcom")
    @RequiresPermissions("supcom:del")
    public ResultVo delcom(Integer id){
        try {
            Supplier supplier = supplierService.getById(id);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("删除供应商货物:"+supplier.getName());
            journal.setMenuId(11);
            journalService.add(journal);
            supComService.delete(id);
            return ResultVo.sucess("删除成功！！");
        }catch (Exception e){
            return ResultVo.fail("删除失败！",e);
        }
    }
    @PostMapping("/getc/{nameId}")
    public ResultVo getc(@PathVariable Integer nameId){
        System.out.println(nameId);
        return ResultVo.sucess(supComService.getById(nameId));
    }
}
